package com.struts.utils;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2015/2/6.
 */
public class XmlCreate {


    public  static void add()throws Exception{
        SAXBuilder saxBuilder = new SAXBuilder();
        Document document = saxBuilder.build(new File("E:\\ITEM\\demo\\demo_struts\\web\\WEB-INF/struts-config.xml"));
        //拿到根节点
        Element element =document.getRootElement();
        System.out.println("根root:"+element.getName());
        //拿到子节点
        Element elementForm = element.getChild("form-beans");

        System.out.println("child:"+elementForm.getName());

        //拿到孙子节点
        List<Element> beanForm = elementForm.getChildren();
            System.out.println(beanForm);
        //循环子节点里面的所有
        for (Element e : beanForm){

            String s = e.getAttributeValue("name");
            String s1 = e.getAttributeValue("type");
            System.out.println(s+"||"+s1);

        }

        Element elementroot = element.getChild("action-mappings");
        System.out.println("||"+elementroot.getName());
        List<Element> action = elementroot.getChildren();
        System.out.println(action);
        for (Element e : action){

            String name1 = e.getAttributeValue("name");
            String path = e.getAttributeValue("path");
            String type = e.getAttributeValue("type");
            System.out.println("action:"+name1+"||"+path+"||"+type);
           List<Element> actionfword = e.getChildren();
            for (Element el: actionfword){
                String name2 = el.getAttributeValue("name");
                String value = el.getAttributeValue("value");
               // String path1 = el.getAttributeValue("path");
                System.out.println("fowrd:"+name2+value);

            }
            System.out.println("action:"+name1+path+type);
        }






    }





}
